import { EXIT, FLASH_USER, FLASH_EXAM, GETUSERDATA, MESSAGENUM, FLASH_MOB_STATUS } from './mutations-types'
import { getCookie, clearCookie, removeStore, setCookie } from '@/untils/storage'
export default{
  [EXIT] (state) {
    clearCookie('Token') // token值
    clearCookie('Exam')
    removeStore('selectedProList') // 退出后 确保本地选择商品被删除
    clearCookie('getTokenTime')
    clearCookie('username')
    clearCookie('needUpdateStatus')
    clearCookie('cookieInfo')
    state.isLogin = false // 登录状态
    state.isExam = '' // 认证状态
    state.username = '' // 用户名
  },
  [FLASH_USER] (state) {
    const token = getCookie('Token')
    const username = getCookie('username')
    if (token) {
      state.isLogin = true
      state.username = username
    } else {
      state.isLogin = false
    }
  },
  [FLASH_EXAM] (state, status) {
    if (status) {
      state.isExam = status
    }
  },
  [FLASH_MOB_STATUS] (state, obj) {
    state.mobile = obj.mobile
  },
  [GETUSERDATA] (state, info) {
    console.log(info)
    if (info.data && info.data.resultCode === 2000) {
      const data = info.data.data
      state.username = data.username
      state.isExam = data.status
      state.isLogin = true
      state.mobile = data.mobile
      state.messageNum = data.messageNumber
      state.headImg = data.headImg
      state.acceptanceAmount = data.acceptanceAmount || 0
      setCookie('Exam', data.status)
    } else if (info.data.resultCode === 3001) {
      state.username = ''
      state.isExam = ''
      state.mobile = ''
      state.headImg = ''
      state.acceptanceAmount = ''
      clearCookie('Token')
      clearCookie('Exam')
    }
  },
  [MESSAGENUM] (state, num) {
    if (num) {
      state.messageNum = num
    }
  }
}
